package com.investment.advisor.controller;

import com.investment.advisor.dto.TradeDTO;
import com.investment.advisor.service.TradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/trades")
@Api(tags = "交易管理", description = "交易的增删改查")
public class TradeController {

    private final TradeService tradeService;

    @Autowired
    public TradeController(TradeService tradeService) {
        this.tradeService = tradeService;
    }

    @GetMapping
    @ApiOperation("获取所有交易")
    public ResponseEntity<List<TradeDTO>> getAllTrades() {
        return ResponseEntity.ok(tradeService.getAllTrades());
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取交易")
    public ResponseEntity<TradeDTO> getTradeById(
            @ApiParam(value = "交易ID", required = true)
            @PathVariable Long id) {
        return ResponseEntity.ok(tradeService.getTradeById(id));
    }

    @GetMapping("/portfolio/{portfolioId}")
    @ApiOperation("根据组合ID获取交易")
    public ResponseEntity<List<TradeDTO>> getTradesByPortfolioId(
            @ApiParam(value = "组合ID", required = true)
            @PathVariable Long portfolioId) {
        return ResponseEntity.ok(tradeService.getTradesByPortfolioId(portfolioId));
    }

    @GetMapping("/fund/{fundId}")
    @ApiOperation("根据基金ID获取交易")
    public ResponseEntity<List<TradeDTO>> getTradesByFundId(
            @ApiParam(value = "基金ID", required = true)
            @PathVariable Long fundId) {
        return ResponseEntity.ok(tradeService.getTradesByFundId(fundId));
    }

    @GetMapping("/portfolio/{portfolioId}/fund/{fundId}")
    @ApiOperation("根据组合ID和基金ID获取交易")
    public ResponseEntity<List<TradeDTO>> getTradesByPortfolioAndFund(
            @ApiParam(value = "组合ID", required = true)
            @PathVariable Long portfolioId,
            @ApiParam(value = "基金ID", required = true)
            @PathVariable Long fundId) {
        return ResponseEntity.ok(tradeService.getTradesByPortfolioAndFund(portfolioId, fundId));
    }

    @GetMapping("/action/{action}")
    @ApiOperation("根据交易类型获取交易")
    public ResponseEntity<List<TradeDTO>> getTradesByAction(
            @ApiParam(value = "交易类型", required = true)
            @PathVariable String action) {
        return ResponseEntity.ok(tradeService.getTradesByAction(action));
    }

    @GetMapping("/status/{status}")
    @ApiOperation("根据交易状态获取交易")
    public ResponseEntity<List<TradeDTO>> getTradesByStatus(
            @ApiParam(value = "交易状态", required = true)
            @PathVariable String status) {
        return ResponseEntity.ok(tradeService.getTradesByStatus(status));
    }

    @GetMapping("/creator/{creator}")
    @ApiOperation("根据创建者获取交易")
    public ResponseEntity<List<TradeDTO>> getTradesByCreator(
            @ApiParam(value = "创建者", required = true)
            @PathVariable String creator) {
        return ResponseEntity.ok(tradeService.getTradesByCreator(creator));
    }

    @GetMapping("/date-range")
    @ApiOperation("根据日期范围获取交易")
    public ResponseEntity<List<TradeDTO>> getTradesByDateRange(
            @ApiParam(value = "开始日期", required = true)
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @ApiParam(value = "结束日期", required = true)
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        return ResponseEntity.ok(tradeService.getTradesByDateRange(startDate, endDate));
    }

    @GetMapping("/portfolio/{portfolioId}/date-range")
    @ApiOperation("根据组合ID和日期范围获取交易")
    public ResponseEntity<List<TradeDTO>> getTradesByPortfolioAndDateRange(
            @ApiParam(value = "组合ID", required = true)
            @PathVariable Long portfolioId,
            @ApiParam(value = "开始日期", required = true)
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
            @ApiParam(value = "结束日期", required = true)
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        return ResponseEntity.ok(tradeService.getTradesByPortfolioAndDateRange(portfolioId, startDate, endDate));
    }

    @PostMapping
    @ApiOperation("创建交易")
    public ResponseEntity<TradeDTO> createTrade(
            @ApiParam(value = "交易信息", required = true)
            @RequestBody TradeDTO tradeDTO) {
        return new ResponseEntity<>(tradeService.createTrade(tradeDTO), HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新交易")
    public ResponseEntity<TradeDTO> updateTrade(
            @ApiParam(value = "交易ID", required = true)
            @PathVariable Long id,
            @ApiParam(value = "交易信息", required = true)
            @RequestBody TradeDTO tradeDTO) {
        return ResponseEntity.ok(tradeService.updateTrade(id, tradeDTO));
    }

    @PatchMapping("/{id}/status")
    @ApiOperation("更新交易状态")
    public ResponseEntity<TradeDTO> updateTradeStatus(
            @ApiParam(value = "交易ID", required = true)
            @PathVariable Long id,
            @ApiParam(value = "交易状态", required = true)
            @RequestBody Map<String, String> statusUpdate) {
        String status = statusUpdate.get("status");
        if (status == null) {
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(tradeService.updateTradeStatus(id, status));
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除交易")
    public ResponseEntity<Void> deleteTrade(
            @ApiParam(value = "交易ID", required = true)
            @PathVariable Long id) {
        tradeService.deleteTrade(id);
        return ResponseEntity.noContent().build();
    }
} 